import 'package:flutter/material.dart';
import 'package:ui_templates/app_theme.dart';
import 'package:ui_templates/drawer_row/home_drawer.dart';

class DrawerRow extends StatefulWidget {
  const DrawerRow({
    super.key,
    required this.onIndexTap,
    required this.currentPage,
    required this.drawerWidth,
    required this.currentIndex,
  });

  final Widget currentPage;
  final Function(DrawerIndex index) onIndexTap;
  final double drawerWidth;
  final DrawerIndex currentIndex;

  @override
  State<DrawerRow> createState() => _DrawerRowState();
}

class _DrawerRowState extends State<DrawerRow> with TickerProviderStateMixin {
  late AnimationController xMoveController;
  late ScrollController singleController;
  double curOffset = 270;
  double scrolloffset = 0;

  @override
  void initState() {
    // TODO: implement initState
    double drawerWidth = 270;

    //要动画控制器，主要是小于某一个阈值我们要自动关闭，自动开启
    //scrolloffset用来标记drawer是开的还是关的

    xMoveController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 500));
    singleController = ScrollController(initialScrollOffset: drawerWidth);

    singleController.addListener(() {
      //开关的逻辑被封装到里面抽屉自身了
      curOffset = singleController.offset;
      if (singleController!.offset == 0) {
        //已经完全打开
        setState(() {
          scrolloffset = 1.0;
          try {} catch (_) {}
        });
        xMoveController.animateTo(0.0,
            duration: const Duration(milliseconds: 0),
            curve: Curves.fastOutSlowIn);
      } else if (singleController!.offset > 0 &&
          singleController!.offset < drawerWidth.floor()) {
        print("未完全打开时动画的状态：${xMoveController.status}");
        xMoveController.animateTo((singleController!.offset / (drawerWidth)),
            duration: const Duration(milliseconds: 0),
            curve: Curves.fastOutSlowIn);
      } else {
        //270就是全部关闭了
        setState(() {
          scrolloffset = 0.0;
        });
        xMoveController.animateTo(1.0,
            duration: const Duration(milliseconds: 1000),
            curve: Curves.fastOutSlowIn);
      }
      print("scrolloffset:$scrolloffset");
    });
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      singleController.jumpTo(
        widget.drawerWidth,
      );
    });
    super.initState();
  }

  @override
  void dispose() {
    xMoveController.dispose();
    singleController.dispose();
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double drawerWidth = MediaQuery.of(context).size.width * 0.75;
    double normalWidth = MediaQuery.of(context).size.width;
    double normalHeight = MediaQuery.of(context).size.height;
    // print("$drawerWidth,   $normalWidth,   $normalHeight");
    return Scaffold(
      backgroundColor: AppTheme.white,
      // floatingActionButton: FloatingActionButton(
      //   child: Text("close"),
      //   onPressed: () {
      //     xMoveController.forward();
      //     print(xMoveController.status);
      //     singleController.animateTo(widget.drawerWidth,
      //         duration: Duration(seconds: 1), curve: Curves.easeIn);
      //   },
      // ),
      body: Scrollbar(
        controller: singleController,
        child: SingleChildScrollView(
          controller: singleController,
          scrollDirection: Axis.horizontal,
          child: SizedBox(
            width: widget.drawerWidth + normalWidth,
            height: normalHeight,
            child: Row(
              children: [
                Container(
                  width: drawerWidth,
                  height: normalHeight,
                  child: AnimatedBuilder(
                      animation: xMoveController,
                      builder: (BuildContext context, Widget? child) {
                        return Transform(
                          transform: Matrix4.translationValues(curOffset, 0, 0),
                          child: HomeDrawer(
                              index: widget.currentIndex,
                              onSelectedChange: widget.onIndexTap,
                              animation: xMoveController,
                              closeHandle: () {
                                xMoveController.forward();
                                print(xMoveController.status);
                                singleController.animateTo(widget.drawerWidth,
                                    duration: Duration(milliseconds: 500),
                                    curve: Curves.easeIn);
                              }),
                        );
                      }),
                ),
                Stack(
                  children: [
                    Container(
                      width: normalWidth,
                      color: Colors.cyan,
                      child: widget.currentPage,
                    ),
                    Positioned(
                      top: AppBar().preferredSize.height - 30,
                      left: 10,
                      child: IconButton(
                        icon: AnimatedIcon(
                          icon: AnimatedIcons.arrow_menu,
                          progress: xMoveController,
                        ),
                        onPressed: () {
                          if (scrolloffset == 0) {
                            print("if");
                            singleController.animateTo(0,
                                duration: Duration(milliseconds: 500),
                                curve: Curves.easeInOut);
                          } else {
                            print("else");
                            singleController.animateTo(widget.drawerWidth,
                                duration: Duration(milliseconds: 500),
                                curve: Curves.easeInOut);
                          }
                        },
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
